package java8.optional;

import com.alibaba.fastjson.JSON;
import entity.User;
import org.junit.Test;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2021/2/27
 * @Version：1.0.0
 */
public class OptionalTestOne {

    private static final Logger logger = Logger.getLogger(String.valueOf(OptionalTestOne.class));

    @Test
    public void test1() {
        User user = new User(1, "张三", "西安");
        Optional<String> name = Optional.of(user.getName());
        logger.info("=====> " + name.get());
        user.setName(null);
        // name不为空 否则orelse
        String testname = Optional.ofNullable(user.getName()).orElse("李四");
        logger.info(testname);

        assert (testname).equals("李四");
        logger.info("====> testname等于李四");

    }

    @Test
    public void test2() {
        User user = new User(1, "张三", "西安");
        Optional<User> emptyOpt = Optional.of(user);
        User user1 = emptyOpt.get();
        logger.info(JSON.toJSONString(user1));
    }


    @Test
    public void test3() {
        User user1 = new User(1, "张三", "西安");
        User user2 = new User(2, "李四", "新疆");

        user1 = null;
        User user = Optional.ofNullable(user1).orElseGet(() -> user2);
        logger.info(user.toString());
    }

    @Test
    public void test4() {
        User user = new User(1, "张三", "西安");
        user = null;
        User result = Optional.ofNullable(user).orElseThrow(() -> new IllegalArgumentException());
    }

    @Test
    public void test5() {
        User user = new User(1, "zhnag", "西安");
        boolean present = Optional.of(user).isPresent();
        logger.info(String.valueOf(present));
    }

    @Test
    public void test6() {
        User user = new User(1, "zhangSan_LISI", "西安");
        String names = Optional.of(user)
                .map(u -> u.getName())
                .map(name -> name.toLowerCase())
                .map(str -> str.replace("_", ""))
                .orElseThrow(() -> new NullPointerException("空指针异常"));
        logger.info(names);
    }

    /**
     * optional 避免集合空指针
     */
    @Test
    public void test7() {
        List<String> list2 = null;
        List<String> list1 = Optional.ofNullable(list2).orElse(new ArrayList<>());
        for (String s : list1) {
            System.out.println(s);
        }
        list1.add("test");
        list1.addAll(Optional.ofNullable(list2).orElse(new ArrayList<>()));
        logger.info("tets!");
    }

    @Test
    public void test8() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("list", null);

        List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
        for (Map<String, Object> item : list) {
            logger.info(String.valueOf(item));
        }
    }

    @Test
    public void test9() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("list", null);

        List<Map<String, Object>> list = Optional.ofNullable((List<Map<String, Object>>) map.get("list")).orElse(new ArrayList<>());
        for (Map<String, Object> item : list) {
            logger.info(String.valueOf(item));
        }
    }

}
